package secretaryGUI;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.table.JTableHeader;

import secretary.defineSemester;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.userData;
import guiCode.secretaryScreenCode;

public class handleUpperTableEvents {

	secretaryScreenCode ssc;
	secretaryAlerts sa = new secretaryAlerts();
	private userData classData = new userData();
	private userData preConditions = new userData();
	private userData students = new userData();
	private userData courses = new userData();
	private int classID;
	private int studentID;
	private int semesterReal;
	private CachedRowSetImpl crs2;
	private int report=0;
	private String index, tableName = "empty";
	updateUpperTableEntries UUTE = new updateUpperTableEntries();
	updateLowerTableEntries ULTE = new updateLowerTableEntries();
	defineSemester ds = new defineSemester();

	public void click(secretaryScreenCode code, createSecretaryWindow csw,
			Point point, ClientConsole secretaryConnection,
			CachedRowSetImpl crs, int semester, int classToEdit) {
		// semester is the relevant semester we are working on
		ssc = code;
		csw.row = csw.table.rowAtPoint(point);
		csw.col = csw.table.columnAtPoint(point);

		semesterReal = ds.identify(ssc.getSemesterForEdit(), secretaryConnection, crs);

		if (ssc.getEditMode() == 13) {
			// this is the manual removal of a student from a course
			courses.deleteAll();
			courses.addData(420);
			courses.addData(1);
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// this
																					// is
																					// the
																					// student
																					// id
			courses.addData(semesterReal);// this is the semester
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			// this will show the student's courses
			ULTE.update(csw, crs);
			courses.deleteAll();
			courses.addData(481);
			courses.addData(1);
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// this
																					// is
																					// the
																					// student
																					// id
			courses.addData(semesterReal);// this is the semester
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.RED);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.GREEN);
		}

		if (ssc.getEditMode() == 12) {
			// remove course from teacher
			// this will remove the course
			courses.deleteAll();
			courses.addData(416);
			courses.addData(0);
			courses.addData(ssc.getTeacherID());
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));
			courses.addData(semesterReal);
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();

			// reduce the teaching hours of the teacher
			classData.deleteAll();
			classData.addData(409);
			classData.addData(1);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			try {
				crs.next();
				int reduceHours = Integer.parseInt(crs.getString(2));// how much
																		// hours
																		// to
																		// reduce
				// get the teacher hours
				classData.deleteAll();
				classData.addData(406);
				classData.addData(1);
				classData.addData(ssc.getTeacherID());
				classData.addData(ds.identify(ssc.getSemesterForEdit(),
						secretaryConnection, crs));
				secretaryConnection.acceptMsg(classData);
				crs = secretaryConnection.getResults();
				crs.next();
				reduceHours = Integer.parseInt(crs.getString(6)) - reduceHours;// new
																				// hours
																				// for
																				// the
																				// teacher
				classData.deleteAll();
				classData.addData(413);
				classData.addData(0);
				classData.addData(reduceHours);
				classData.addData(ssc.getTeacherID());
				secretaryConnection.acceptMsg(classData);
				crs = secretaryConnection.getResults();
				classData.deleteAll();
				classData.addData(406);
				classData.addData(1);
				classData.addData(ssc.getTeacherID());
				classData.addData(ds.identify(ssc.getSemesterForEdit(),
						secretaryConnection, crs));
				secretaryConnection.acceptMsg(classData);
				crs = secretaryConnection.getResults();
				ULTE.update(csw, crs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// update the display
			classData.deleteAll();
			classData.addData(415);
			classData.addData(1);
			classData.addData(ssc.getTeacherID());
			classData.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);

		}

		if (ssc.getEditMode() == 11) {
			// this is to add course to teacher
			// check if the teacher already assigned to this course
			courses.deleteAll();
			courses.addData(408);
			courses.addData(1);
			courses.addData(ssc.getTeacherID());
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));
			courses.addData(semesterReal);
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == true) {
					// teacher already assigned to this course in this semester
					sa.showMessages(csw, ssc, csw.secretaryScreen,
							"Teacher is already assigned to this course!",
							"Assign teacher to course - failure", 1);
				} else {
					// teacher can be assigned to this course, first check the
					// teaching unit
					courses.deleteAll();
					courses.addData(409);
					courses.addData(1);
					courses.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0));
					courses.addData(semesterReal);
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					crs.next();
					int courseHours = Integer.parseInt(crs.getString(2));
					// check the tachening unit
					courses.deleteAll();
					courses.addData(410);
					courses.addData(1);
					courses.addData(ssc.getTeacherID());
					//courses.addData(semesterReal);
					courses.addData(crs.getString(1));
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					if (crs.next()==false) {
						// teacher does not belong to the right teaching unit
						sa.showMessages(
								csw,
								ssc,
								csw.secretaryScreen,
								"Teacher doesnt belong to the course's teaching unit",
								"Assign teacher to course - Failure", 1);
					} else {
						// check if teaching hours was exceeded
						courses.deleteAll();
						courses.addData(411);
						courses.addData(1);
						courses.addData(ssc.getTeacherID());
						secretaryConnection.acceptMsg(courses);
						crs = secretaryConnection.getResults();
						crs.next();
						if (courseHours + Integer.parseInt(crs.getString(2)) > Integer
								.parseInt(crs.getString(1))) {
							sa.showMessages(
									csw,
									ssc,
									csw.secretaryScreen,
									"Teaching hours limitation for this teacher was exceeded",
									"Assign teacher to course - failure", 1);
						} else {
							courseHours = courseHours
									+ Integer.parseInt(crs.getString(2));
							// register teacher to the course
							courses.deleteAll();
							courses.addData(412);
							courses.addData(0);
							courses.addData(csw.table.getValueAt(
									csw.table.rowAtPoint(point), 0));
							courses.addData(ssc.getTeacherID());
							courses.addData(semesterReal);
							secretaryConnection.acceptMsg(courses);
							crs = secretaryConnection.getResults();
							// reduce the teaching houurs
							courses.deleteAll();
							courses.addData(413);
							courses.addData(0);
							courses.addData(courseHours);
							courses.addData(ssc.getTeacherID());
							secretaryConnection.acceptMsg(courses);
							crs = secretaryConnection.getResults();
							// update the display
							courses.deleteAll();
							courses.addData(414);
							courses.addData(1);
							courses.addData(ssc.getTeacherID());
							courses.addData(semesterReal);
							secretaryConnection.acceptMsg(courses);
							crs = secretaryConnection.getResults();
							ULTE.update(csw, crs);
						}
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (ssc.getEditMode() == 10) {
			// remove course from semester
			courses.deleteAll();
			courses.addData(324);
			courses.addData(0);
			courses.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// courseID
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			// remove classes that were already assigned with this course
			courses.deleteAll();
			courses.addData(325);
			courses.addData(0);
			courses.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// courseID
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			// remove students who are assigned already to this course
			courses.deleteAll();
			courses.addData(326);
			courses.addData(0);
			courses.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// courseID
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			//remove course from coureses table
			courses.deleteAll();
			courses.addData(566);
			courses.addData(0);
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// courseID
			courses.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			// update display
			ssc.viewCourses();
		}
		if (ssc.getEditMode() == 9) {
			// mode 9 assigning new courses to semester (courses with semester
			// id of 0)
			// check if the course is already registered to this semester
			courses.deleteAll();
			courses.addData(321);
			courses.addData(1);
			courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));// courseID
			courses.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(courses);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next()) {
					// course is already registered with this semester
					sa.showMessages(csw, code, csw.secretaryScreen,
							"This course is already assigned to this semester",
							"Course assignment for semester - Failure", 1);
				} else {
					courses.deleteAll();
					courses.addData(564);
					courses.addData(1);
					courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					crs.next();
					courses.deleteAll();
					courses.addData(565);
					courses.addData(0);
					courses.addData(csw.table.getValueAt(csw.table.rowAtPoint(point), 0));
					courses.addData(crs.getString(2));
					courses.addData(crs.getString(3));
					courses.addData(crs.getString(4));
					courses.addData(crs.getString(5));
					courses.addData(ds.identify(ssc.getSemesterForEdit(), secretaryConnection, crs));
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					courses.deleteAll();
					courses.addData(322);
					courses.addData(0);
					courses.addData(ds.identify(ssc.getSemesterForEdit(),
							secretaryConnection, crs));
					courses.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0));// courseID
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					// update display
					courses.deleteAll();
					courses.addData(259);
					courses.addData(1);
					courses.addData(ds.identify(ssc.getSemesterForEdit(),
							secretaryConnection, crs));
					secretaryConnection.acceptMsg(courses);
					crs = secretaryConnection.getResults();
					UUTE.update(csw, crs);
					switch (ssc.getSemesterForEdit()) {
					case 0: // this is the current semester
						courses.deleteAll();
						courses.addData(260);
						courses.addData(1);
						courses.addData(ds
								.identify(0, secretaryConnection, crs));
						secretaryConnection.acceptMsg(courses);
						crs = secretaryConnection.getResults();
						ULTE.update(csw, crs);
						break;
					case 1:// this is the next semester
						courses.deleteAll();
						courses.addData(260);
						courses.addData(1);
						courses.addData(ds
								.identify(1, secretaryConnection, crs));
						secretaryConnection.acceptMsg(courses);
						crs = secretaryConnection.getResults();
						ULTE.update(csw, crs);
						break;
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (ssc.getEditMode() == 8) {
			// this will check if the student already registered to this course
			classData.deleteAll();
			classData.addData(253);
			classData.addData(1);
			classData.addData(ssc.getStudentID());
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			classData.addData(ds.identify(semester, secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == true) {
					sa.showMessages(csw, code, csw.secretaryScreen,
							"Student is already registered to this course!",
							"Student registration to course - failure", 1);
				} else {
					// user is not registered to this course, we will check pre
					// conditions
					classData.deleteAll();
					classData.addData(254);
					classData.addData(1);
					classData.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0));
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();// crs now has all
															// preconditions for
															// the course
					try {
						int missing;// missing pre condition
						missing = 0;
						while (crs.next() == true) {
							classData.deleteAll();
							classData.addData(255);
							classData.addData(1);
							classData.addData(ssc.getStudentID());
							classData.addData(crs.getString(1));
							;
							secretaryConnection.acceptMsg(classData);
							crs2 = secretaryConnection.getResults();
							if (crs2.next() == false) {
								// student has pre condition missing
								missing = 1;
							}
						}
						switch (missing) {
						case 0:// has all pre conditions
							classData.deleteAll();
							classData.addData(256);
							classData.addData(0);
							classData.addData(csw.table.getValueAt(
									csw.table.rowAtPoint(point), 0));
							classData.addData(ssc.getStudentID());
							classData.addData(ds.identify(semester,
									secretaryConnection, crs));
							classData.addData(0);
							secretaryConnection.acceptMsg(classData);
							crs2 = secretaryConnection.getResults();
							System.out.println("missing = " + missing);
							classData.deleteAll();
							classData.addData(252);
							classData.addData(1);
							classData.addData(ssc.getStudentID());
							secretaryConnection.acceptMsg(classData);
							crs2 = secretaryConnection.getResults();
							ULTE.update(csw, crs2);
							break;
						case 1:// student has missing pre condition
							sa.showMessages(
									csw,
									code,
									csw.secretaryScreen,
									"Student has pre conditions to accomplish before you can register him to this course!",
									"Course registration - failure", 1);
							break;
						}

					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (ssc.getEditMode() == 7) {
			// this will remove courses from classes
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.RED);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.GREEN);
			classData.deleteAll();
			classData.addData(248);
			classData.addData(1);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			csw.lblDOWN
					.setText("These are the courses that are assigned to class "
							+ csw.table.getValueAt(csw.table.rowAtPoint(point),
									2) + ":");
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}
		if (ssc.getEditMode() == 6) {
			// this will check if the course is already is assigned to this
			// class
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			classData.deleteAll();
			classData.addData(100);
			classData.addData(1);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			classData.addData(classToEdit);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == true) {
					sa.showMessages(csw, ssc, csw.secretaryScreen,
							"class is already assigned to this course",
							"Add course to class - Failure", 1);
				} else {
					// this is to add courses to class
					classData.deleteAll();
					classData.addData(99);
					classData.addData(0);
					classData.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0));// this is the
																// course id
					classData.addData(classToEdit);
					classData.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 1));// this is the
																// course name
					classData.addData(semesterReal);
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					// this will update the display
					classData.deleteAll();
					classData.addData(98);
					classData.addData(1);
					classData.addData(classToEdit);
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					ULTE.update(csw, crs);
					System.out.println("this is me");
					// this will check for pre-conditions for this course
					classData.deleteAll();
					classData.addData(161);
					classData.addData(1);
					String courseID = csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0).toString();
					classData.addData(csw.table.getValueAt(
							csw.table.rowAtPoint(point), 0));// this is the
																// course id
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					
					if (crs.next() == true) {
						//course has preconditions
						crs.beforeFirst();
						preConditions.deleteAll();
						students.deleteAll();
						// this will put the course pre conditions into
						// preConditions list
						while (crs.next()) {
							preConditions.addData(crs.getString(2));
							System.out.println(preConditions.getData(0)
									.toString());
						}
						// this will get the students in the class
						classData.deleteAll();
						classData.addData(163);
						classData.addData(1);
						classData.addData(classToEdit);
						classData.addData(ds.identify(ssc.getSemesterForEdit(), secretaryConnection, crs));
						System.out.println("class to edit: " + classToEdit);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
						if (crs.next() == true) {
							// class contains students
							crs.beforeFirst();
							while (crs.next()) {
								
								students.addData(crs.getString(1));
							}
							// this will check if the student has the pre
							// condition
							// in
							// his list of courses
							
							//int report ;// this will indicate if report was
							//report=0;			// created
											// or not
							CachedRowSetImpl crs2 = null;
							//String index, tableName = "empty";
							Calendar reportTime = Calendar.getInstance();
							SimpleDateFormat formatter = new SimpleDateFormat(
									"yyyy/MM/dd");
							for (int i = 0; i < preConditions.getLength(); i++) {
								for (int j = 0; j < students.getLength(); j++) {
									classData.deleteAll();
									classData.addData(164);
									classData.addData(1);
									classData.addData(preConditions.getData(i));
									
									classData.addData(students.getData(j));
									secretaryConnection.acceptMsg(classData);
									crs = secretaryConnection.getResults();
									if (crs.next() == false) {
										//student has no pre condition for this course
										
										
											// creating report
											classData.deleteAll();
											classData.addData(165);
											classData.addData(1);
											secretaryConnection
													.acceptMsg(classData);
											crs2 = secretaryConnection
													.getResults();
											crs2.next();
											
											index = crs2.getString(1);
											System.out.println("prin2t");
											tableName = "nonQualifiedStudents_"
													+ (Integer.parseInt(index) );//+1
											if(report==0){
												report = 1;
												// this will create the table
												classData.deleteAll();
												classData.addData(169);
												classData.addData(0);
												classData
														.addData("report on no qualified students");
												classData.addData(tableName);
												classData.addData(formatter
														.format(reportTime
																.getTime()));
												secretaryConnection
														.acceptMsg(classData);
												crs2 = secretaryConnection
														.getResults();
												classData.deleteAll();
												classData.addData(168);
												classData.addData(0);
												classData.addData(Integer
														.parseInt(index) + 1);
												secretaryConnection
														.acceptMsg(classData);
												crs2 = secretaryConnection
														.getResults();
												//report = 1;
												classData.deleteAll();
												classData.addData(165);
												classData.addData(1);
												secretaryConnection
														.acceptMsg(classData);
												crs2 = secretaryConnection.getResults();
												crs2.next();
												
											}
											
											classData.deleteAll();
											classData.addData(170);
											classData.addData(0);
											tableName = "nonQualifiedStudents_"
													+ (Integer.parseInt(crs2
															.getString(1))); // add
																				// here
																				// minus
																				// 1
											classData.addData(tableName);
											classData.addData(students.getData(j));
											classData.addData(courseID);
											classData.addData(preConditions
													.getData(i));
											classData.addData(formatter
													.format(reportTime.getTime()));
											secretaryConnection
													.acceptMsg(classData);
											crs2 = secretaryConnection.getResults();
									
										
									}// for with j

								}// for with i
									// this will fill in the report the students
									// names
								if(report==1){
									classData.deleteAll();
									classData.addData(171);
									classData.addData(1);
									classData.addData(tableName);
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									while (crs2.next()) {
										students.addData(crs2.getString(1));// this will
																			// get
																			// the
																			// students
																			// id
																			// in
																			// the
																			// report
									}
									for ( i = 0; i < students.getLength(); i++) {
										classData.deleteAll();
										classData.addData(172);
										classData.addData(1);
										classData.addData(students.getData(i));
										secretaryConnection.acceptMsg(classData);
										crs2 = secretaryConnection.getResults();
										classData.deleteAll();
										classData.addData(173);
										classData.addData(0);
										classData.addData(tableName);
										crs2.next();
										classData.addData(crs2.getString(1));
										classData.addData(crs2.getString(2));
										classData.addData(students.getData(i));
										secretaryConnection.acceptMsg(classData);
										crs2 = secretaryConnection.getResults();
									}
									// this will fill the course name in the report
									classData.deleteAll();
									classData.addData(174);// this will get the courseID
									classData.addData(1);
									classData.addData(tableName);
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									crs2.next();
									courseID = crs2.getString(1);
									classData.deleteAll();
									classData.addData(175);// this will get the
															// courseName
									classData.addData(1);
									classData.addData(crs2.getString(1));
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									crs2.next();
									String courseName = crs2.getString(1);
									for ( i = 0; i < students.getLength(); i++) {
										classData.deleteAll();
										classData.addData(176);// this will update the
																// courseName in the
																// report
										classData.addData(0);
										classData.addData(tableName);
										classData.addData(courseName);
										classData.addData(courseID);
										secretaryConnection.acceptMsg(classData);
										crs2 = secretaryConnection.getResults();
									}
									// this will update the pre condition course name in
									// the
									// report
									classData.deleteAll();
									classData.addData(161);// this will get the
															// courseName
									classData.addData(1);
									classData.addData(courseID);
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									crs2.next();
									courseID = crs2.getString(2);// this is the pre
																	// course
																	// ID
									classData.deleteAll();
									classData.addData(175);// this will get the pre
															// course
															// courseName
									classData.addData(1);
									classData.addData(courseID);
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									crs2.next();
									courseName = crs2.getString(1);// this is the pre
																	// course
																	// ID
									for ( i = 0; i < students.getLength(); i++) {
										classData.deleteAll();
										classData.addData(177);// this will update the
																// courseName in the
																// report
										classData.addData(0);
										classData.addData(tableName);
										classData.addData(courseName);
										classData.addData(courseID);
										secretaryConnection.acceptMsg(classData);
										crs2 = secretaryConnection.getResults();
									}
									preConditions.deleteAll();
									students.deleteAll();
									//report = 0;
									/* this will activate the view errors button */
									csw.btnViewErrors.setEnabled(true);
									csw.btnViewErrors.setForeground(Color.RED);
									classData.deleteAll();
									classData.addData(242);
									classData.addData(1);
									classData.addData(tableName);
									secretaryConnection.acceptMsg(classData);
									crs2 = secretaryConnection.getResults();
									crs2.next();
									csw.btnViewErrors
											.setText(csw.btnViewErrors.getText() + " ("
													+ crs2.getString(1) + ")");
									sa.showMessages(
											csw,
											ssc,
											csw.secretaryScreen,
											"There were erros in assigning the course to the class.\nClick on view assigining errors to view the error log.",
											"Assigning errors", 1);
											}
									
								/*
								ssc.setLastAssigningErrorLog(tableName);
								report=0;
								System.out.println("i am am am am here"+tableName);
								ssc.assignStudentToCourse(tableName, csw.table
										.getValueAt(csw.table.rowAtPoint(point), 0)
										.toString(), semesterReal, classToEdit, 1,
										ds.identify(ssc.getSemesterForEdit(),
												secretaryConnection, crs2));*/
								}
								
						} 
						ssc.setLastAssigningErrorLog(tableName);
						report=0;
						System.out.println("i am am am am here"+tableName);
						ssc.assignStudentToCourse(tableName, csw.table
								.getValueAt(csw.table.rowAtPoint(point), 0)
								.toString(), semesterReal, classToEdit, 1,
								ds.identify(ssc.getSemesterForEdit(),
										secretaryConnection, crs2));
						tableName="empty";
					}else {
						report=0;
							System.out.println("no pre conditions!");
							ssc.assignStudentToCourse("empty", csw.table
									.getValueAt(csw.table.rowAtPoint(point), 0)
									.toString(), semesterReal, classToEdit, 1,
									ds.identify(ssc.getSemesterForEdit(),
											secretaryConnection, crs));
						}
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (ssc.getEditMode() == 5) {
			// this will remove student from the class
			classData.deleteAll();
			classData.addData(91);
			classData.addData(0);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			classData.deleteAll();
			classData.addData(166);
			classData.addData(0);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			// this will add it to the temp student table
			classData.deleteAll();
			classData.addData(92);
			classData.addData(0);
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					0));
			studentID = Integer.parseInt(csw.table.getValueAt(
					csw.table.rowAtPoint(point), 0).toString());
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					1));
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					2));
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					3));
			classData.addData(csw.table.getValueAt(csw.table.rowAtPoint(point),
					4));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();

			// this will update the lower table with the student that were
			// removed
			classData.deleteAll();
			classData.addData(93);
			classData.addData(1);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			System.out.println("the semeste is " + semester);
			semesterReal = ds.identify(semester, secretaryConnection, crs);
			ULTE.update(csw, crs);
			// this will update the student details to be free student

			classData.deleteAll();
			classData.addData(87);
			classData.addData(1);
			classData.addData(semesterReal);
			classData.addData(classID);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
		}
		if (ssc.getEditMode() == 3) {
			// this is for edit class mode
			if (csw.col == 1) {
				csw.alertsLbl.setText("This field is not editable!");
				csw.alertsLbl.setForeground(Color.RED);
			}
			if (csw.col == 2) {
				csw.alertsLbl.setForeground(Color.BLUE);
				csw.alertsLbl
						.setText("Click on classID field to add/remove students from class. Click on className to change the class name");
				sa.showMessages(csw, ssc, csw.secretaryScreen, "", "", 5);
			}
			if (csw.col == 0) {
				csw.lblUP.setText("You are editing "
						+ csw.table.getValueAt(csw.table.rowAtPoint(point), 2)
								.toString() + " class profile");
				csw.alertsLbl
						.setText("Click on 'Add students to class' to add more students to this class. Click on 'Remove student from class' to remove students from this class");
				ssc.setEditMode(2);
				csw.btnAddStudentTo.setEnabled(true);
				csw.btnRemoveStudentFrom.setEnabled(true);
				csw.alertsLbl.setForeground(Color.BLUE);

				classData.deleteAll();
				classID = Integer.parseInt((csw.table.getValueAt(csw.row,
						csw.col).toString()));// this will get the classID
				if (ssc.getSemesterForEdit() == 1) {// editing next semester
					classData.addData(15);
					classData.addData(1);// this is for read
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						classData.deleteAll();
						classData.addData(87);
						classData.addData(1);// this is for read
						classData.addData(crs.getString(1));
						classData.addData(classID);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (ssc.getSemesterForEdit() == 0) {// editing current semester
					classData.addData(82);
					classData.addData(1);// this is for read
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						classData.deleteAll();
						classData.addData(87);
						classData.addData(1);// this is for read
						classData.addData(crs.getString(1));
						classData.addData(classID);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				try {
					if (crs.next() == false) {// check if class has already
												// students in it
						csw.btnRemoveStudentFrom.setEnabled(false);
						crs.beforeFirst();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				UUTE.update(csw, crs);

			}
		}
	}
}
